package nuc.edu.employmentservices.controller;

import com.mysql.cj.Session;
import nuc.edu.employmentservices.enity.Graduates;
import nuc.edu.employmentservices.enity.User;
import nuc.edu.employmentservices.service.GraduatesService;
import nuc.edu.employmentservices.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
public class UserController {

    @Autowired
    UserService userService;

    @Autowired
    GraduatesService graduatesService;

    @GetMapping("/getAllUsers")
    public String getAllUsers(Model model){
        List<User> users = userService.getAllUsers();
        model.addAttribute("users",users);
        return "index1";
    }

    @RequestMapping("/login")
    public String login(@RequestParam("uname")  String name,
                        @RequestParam("pws")  String psw,
                        Model model,HttpSession session){

        User user = IsLogin(name,psw);
        if(user.getUserName()!=null){
            session.setAttribute("loginuser",user);
            List<Graduates> graduatesList = graduatesService.getAllGraduates();

            model.addAttribute("graduatesList",graduatesList);
            return "index1";
        }else{
            return "fail";
        }
    }

    @GetMapping("/tologin")
    public String tologin(){
        return "index";
    }

    public User IsLogin(String account,String password){
        User user = new User();
        List<User> users = (List<User>) userService.getAllUsers();
        for(User user2:users){
            if(account.equals(user2.getUseraccount()) && password.equals(user2.getUserPassword())){
                return user2;
            }
        }
        return user;
    }

    @GetMapping("/userlist")
    public String grauser(Model model)
    {
        List<User> userlist=userService.getAllUsers();
        Integer usernum=userService.GetUserNum();
        model.addAttribute("userlist",userlist);
        model.addAttribute("usernum",usernum);
        return "userlist";

    }

    @GetMapping("/toindex")
    public String toindex(Model model)
    {
        List<Graduates> graduatesList=graduatesService.getAllGraduates();
        model.addAttribute("graduatesList",graduatesList);
        return "index1";

    }


    @RequestMapping("/account")
    public String account(){

        return "updateaccount";
    }

    @RequestMapping("/doupdateaccount")
    public String doupdateaccount(User user,HttpSession session){

        User user1 = new User();
        user1 = (User)session.getAttribute("loginuser");
        user.setUserId(user1.getUserId());
        user.setGraID(user1.getGraID());
        user.setTypes(user1.getTypes());
        user.setUserName(user1.getUserName());

        userService.updateUserBy(user);
        session.setAttribute("loginuser",user);
        return "redirect:/dodeletegradutes";
    }

    @RequestMapping("/manageaccount")
    public String manageaccount(Model model){
        List<User> manageUsers = userService.selectUserByType(0);
        model.addAttribute("manageUsers",manageUsers);
        return "/manageaccountlist";
    }

    @RequestMapping("/deletemanageaccount/{deid}")
    public String deletemanageaccount(@PathVariable("deid") Integer deid){
        userService.deleteUserByuserId(deid);
        return  "redirect:/dodeletegradutes";
    }

    @RequestMapping("/toaddmanageaccount")
    public String toaddmanageaccount(){
        return "/toaddmanageaccount";
    }

    @RequestMapping("/addmanageaccount")
    public String addmanageaccount(User user){
        userService.insertUser(user);
        return  "redirect:/dodeletegradutes";
    }

    @RequestMapping("/toupdatemanageaccount/{upid}")
    public String toupdatemanageaccount(@PathVariable("upid") Integer upid, HttpSession session){
        User user = userService.getUserById(upid);
        session.setAttribute("upmauser",user);
        return "redirect:/toIndexup";
    }

    @RequestMapping("/toIndexup")
    public String toIndexup(HttpSession session,Model model){
        User user = (User)session.getAttribute("upmauser");
        model.addAttribute("upuser",user);
        return "/toupdatemanageaccount";
    }

    @RequestMapping("/doupdatemanageaccount")
    public String doupdatemanageaccount(User user){
        userService.updateUserByuserid(user);
        return  "redirect:/dodeletegradutes";
    }


}
